package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.events.houseEvent.OpenHouseEvent;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.HouseModel;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	/**
	 * 
	 * @author Barbiel
	 * 2011-9-10
	 */
	public class SampleRoomTitleWindowMediator extends Mediator
	{		
		[Inject]
		public var view:SampleRoomTitleWindow;
		
		[Inject]
		public var houseModel:HouseModel;
		
		[Inject]
		public var roomModel:ExchangeDataModel;
		public var currentPage:int = 1;
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.sampleHouseList,"dataProvider", houseModel, "sampleHouses");
			
			view.sampleHouseList.addEventListener(IndexChangeEvent.CHANGE, openHouseHandler);
			view.closeBtn.addEventListener(MouseEvent.CLICK, closeBtnClickHandler);
			view.prePageBtn.addEventListener(MouseEvent.CLICK, prePageHandler);
			view.nextPageBtn.addEventListener(MouseEvent.CLICK, nextPageHandler);
		}
		private function prePageHandler(event:MouseEvent):void
		{
			if(currentPage == 1)
			{
				
			}else
			{				
				dispatch(new OpenHouseEvent(OpenHouseEvent.GET_SAMPLE_HOUSE, currentPage - 1, 4, 0));
				currentPage = currentPage - 1;
			}
		}
		
		private function nextPageHandler(event:MouseEvent):void
		{
			dispatch(new OpenHouseEvent(OpenHouseEvent.GET_SAMPLE_HOUSE, currentPage + 1, 4,  0));
			currentPage = currentPage + 1;
		}
		private function openHouseHandler(event:IndexChangeEvent):void
		{
			roomModel.openHouse(new XML(view.sampleHouseList.selectedItem.houseModelSrc));
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
		}
		
		private function closeBtnClickHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
		}
	}
			
}